﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using CoolCode.Web.Mvc.UI;

namespace CoolCode.Web.Mvc
{
	public static class JQueryExtensions
	{
		public static IHtmlWriter JQuery(this AjaxHelper ajax)
		{
			HtmlHelper html = new HtmlHelper(ajax.ViewContext, ajax.ViewDataContainer, ajax.RouteCollection);
			return new JQuery(html, ajax);
		}

		public static IHtmlWriter Ready(this IHtmlWriter writer, Action<IHtmlWriter> function)
		{
			TextWriter output = writer.GetInnerWriter();
			output.WriteLine("<script type=\"text/javascript\">");
			output.WriteLine("$(document).ready(function(){");
			function(writer);
			output.WriteLine("});");
			output.WriteLine("</script>");
			return writer;
		}

		public static IHtmlWriter Load(this IHtmlWriter writer, string url, string updateTarget)
		{
			if (string.IsNullOrEmpty(updateTarget))
				throw new ArgumentNullException("updateTarget");
			UrlHelper urlHelper = new UrlHelper(writer.Html.ViewContext.RequestContext);
			//string action = routeValues == null ? urlHelper.Action(actionName, controllerName) : urlHelper.Action(actionName, controllerName, routeValues, "");

			updateTarget = JQueryHelper.FormatElement(updateTarget);
			string html = string.Format("$('{0}').load('{1}');", updateTarget, url);

			TextWriter output = writer.GetInnerWriter();
			output.Write(html);
			return writer;
		}

		/// <summary>
		///  
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="viewDataKey"></param>
		/// <returns></returns>
		public static Pager Pager(this IHtmlWriter writer, string viewDataKey, string updateTarget)
		{
			var dataSource = writer.Html.ViewContext.ViewData.Eval(viewDataKey) as IPageable;

			if (dataSource == null)
			{
				throw new InvalidOperationException(string.Format("Item in ViewData with key '{0}' is not an IPageable.",
																  viewDataKey));
			}

			return writer.Pager(dataSource, updateTarget);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="source"></param>
		/// <returns></returns>
		public static Pager Pager(this IHtmlWriter writer, IPageable source, string updateTarget)
		{
			var helper = writer.Html;
			JQueryPager pager = new JQueryPager(source, helper.ViewContext.HttpContext, helper.ViewContext.RequestContext, helper.ViewContext.RouteData.Values);
			updateTarget = JQueryHelper.FormatElement(updateTarget);
			return pager.OnClick(string.Format("ajax.post(this.href,'{0}');", updateTarget));
		}

		public class JQueryPager : Pager
		{
			private string _updateTarget;

			public JQueryPager(IPageable source, HttpContextBase httpContext, RequestContext requestContext, RouteValueDictionary routeValues)
				: base(source, httpContext, requestContext, routeValues)
			{
			}

			public JQueryPager UpdateTarget(string element)
			{
				_updateTarget = element;
				return this;
			}

			//protected override string CreateDefaultUrl(int pageIndex)
			//{
			//    return string.Format("javascript:ajax.post('{0}','{1}');", base.CreateDefaultUrl(pageIndex), _updateTarget);
			//}
		}

	}
}
